package lv.imho.client.view;

import java.util.List;

import lv.imho.client.model.DataTransferObject;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Represents status UI component.
 * 
 * @author Igors Gulbinskis
 */
public class OverallStatusPanel extends VerticalPanel {
	
	private final String HEIGHT = "100%";
	private final String WIDTH  = "100%";
	
	private double average;
	
	public OverallStatusPanel(List<DataTransferObject> dataList) {
		super();
		setWidth(WIDTH);
		setHeight(HEIGHT);				
		
		average = 0;
		if (dataList != null) {
			for (DataTransferObject data : dataList) {
				if (data.getEvaluation() != null) {
					average += data.getEvaluation();
				} else {
					average += 0;
				}
			}
			if (dataList.size() > 0) {
				average = average / dataList.size();
			} else {
				average = 0;
			}
		}
		
		initChildComponents();
	}
	
	private void initChildComponents() {				
		Label title = new Label("Kāda ir šī diena kopumā?");		
		title.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);	
		DOM.setStyleAttribute(title.getElement(), "background", "#8cabdf");
		DOM.setStyleAttribute(title.getElement(), "fontWeight", "bold");
		add(title);
		
		VerticalPanel evaluationPanel = new VerticalPanel();
		evaluationPanel.setWidth(WIDTH);
		
		NumberFormat formatter = NumberFormat.getFormat("#.###");		
		Label evaluationMark = new Label(formatter.format(average));
		
		evaluationMark.setHeight(HEIGHT);
		evaluationMark.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);						
		DOM.setStyleAttribute(evaluationMark.getElement(), "fontSize", "80px");
		evaluationPanel.add(evaluationMark);
		
		Label evaluationText = new Label();						
		evaluationText.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);		
		evaluationPanel.add(evaluationText);
		
		/* set style depending on result */
		if (average > 0) {
			evaluationText.setText("Šī diena kopumā ir pozitīva");
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "green");
			DOM.setStyleAttribute(evaluationText.getElement(), "color", "green");
		} else if (average < 0) {
			evaluationText.setText("Šī diena kopumā ir negatīva");
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "red");
			DOM.setStyleAttribute(evaluationText.getElement(), "color", "red");
		} else {
			evaluationText.setText("Šī diena kopumā ir neitrāla");
			DOM.setStyleAttribute(evaluationMark.getElement(), "color", "blue");
			DOM.setStyleAttribute(evaluationText.getElement(), "color", "blue");
		}
		
		add(evaluationPanel);
	}
	
}
